<div class="layuimini-container">
    <div class="layuimini-main">
		<div style="margin: 5px 5px 5px 5px">
			<form class="layui-form layui-form-pane" action="">
				<div class="layui-form-item">
					<div class="layui-inline">
						<div class="layui-input-inline">
							<input type="text" name="tagName" autocomplete="off" class="layui-input" placeholder="标签名称">
							<input type="hidden" name="appId" id="appId"/>
						</div>
					</div>
					<div class="layui-inline">
						<button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
					</div>
				</div>
			</form>
		</div>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 创建标签 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
			<a href="javascript:;" class="layui-font-blue" lay-event="buildVersion">构建版本</a>
			<span style="color: rgb(179, 179, 179);"> | </span>
			<a href="javascript:;" class="layui-font-blue" lay-event="delete">删除</a>
        </script>
    </div>
</div>
<script>

	var branchName;

    layui.use(['form', 'table', 'dropdown', 'miniPage'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
			dropdown = layui.dropdown,
			appId = localStorage.getItem('appId_' + $("#userName").val()),
			miniPage = layui.miniPage;
		
		$("#appId").val(appId);
		
        table.render({
            elem: '#currentTableId',
            url: '/app/tag/page',
			method: 'post',
			contentType: 'application/json;charset=UTF-8',
			request: {
				pageName: 'pageNum',
				limitName: 'pageSize'
			},
			where: {'appId': appId},
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter'],
			parseData: function(res){
				redirectLogin(res.code);
				
				if(res.code != '000000'){
					return {
					  "code": 1,
					  "msg": res.message,
					  "count": 0,
					  "data": []
					};
				}
				
				return {
				  "code": 0,
				  "msg": res.message,
				  "count": res.data.itemCount,
				  "data": res.data.items
				};
			},
            cols: [[
                {field: 'tagName', title: '标签名称'},
                {field: 'updateTime', title: '最后提交时间', width: '13%'},
				{field: 'commitMessage', title: '最后提交信息'},
                {title: '操作', toolbar: '#currentTableBar'}
            ]],
            limits: [10, 20],
            limit: 10,
            page: true,
            skin: 'row',
			even: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                page: {
                    curr: 1
                },
                where: data.field
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '创建标签',
                    type: 2,
                    shade: 0.5,
                    maxmin:false,
                    shadeClose: false,
                    area: ['30%', '40%'],
					offset: ['30%', '35%'],
					content: ['page/tag/add.html', 'no']
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            if (obj.event === 'buildVersion') {
				branchName = obj.data.tagName;
                buildVersion($, appId, obj.data.tagName);
            }else if (obj.event === 'delete') {
                doDelete($, appId, obj.data.tagName);
            }
			return false;
        });
    });
	
	function buildVersion($, appId, branchName){
		$.ajax({
			url: '/app/query',
			type: 'POST',
			dataType: "json",
			contentType: 'application/json;charset=UTF-8',
			data: JSON.stringify({'appId': appId}),
			success: function(res){
				if(!res.data){
					return;
				}
				var techType = res.data.techType;
				//Vue和React
				if(techType == 2 || techType == 3){
					var index = layer.open({
						title: '构建版本',
						type: 2,
						shade: 0.5,
						maxmin: false,
						shadeClose: false,
						area: ['40%', '40%'],
						offset: ['28%', '30%'],
						content: ['page/branch/build_version.html', 'no']
					});
				}else{
					doBuild($, appId, '0', branchName);
				}
			},
			error: function(data){
				layer.msg(data.message, {icon: 5, shift: 6});
			}
		});
	}
	
	//删除
	function doDelete($, appId, tagName) {
		var msg = '确定删除：' + tagName + "？";
		layer.confirm(msg, {
			shade: 0.4,
			maxmin: false,
			shadeClose: false,
			offset: ['35%', '40%']}, function (index) {
			layer.close(index);
			$.ajax({
				url: '/app/tag/delete',
				type: 'POST',
				dataType: "json",
				contentType: 'application/json;charset=UTF-8',
				data: JSON.stringify({"appId": appId, "tagName": tagName}),
				success: function(data){
					if(data.code != "000000"){
						layer.msg(data.message, {icon: 5, shift: 6});
						return false;
					}
					
					layer.msg("删除成功", {
						icon: 1,
						time: 500}, function(){
						layui.table.reload('currentTableId');
					});
				},
				error: function(data){
					layer.msg(data.message, {icon: 5, shift: 6});
				}
			});
		});
	}
</script>